package com.qj.yantaishan.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qj.yantaishan.common.core.domain.R;
import com.qj.yantaishan.common.utils.StringUtils;
import com.qj.yantaishan.domain.Store;
import com.qj.yantaishan.domain.StoreVip;
import com.qj.yantaishan.service.IStoreService;
import com.qj.yantaishan.service.IStoreVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/yantaishan/StoreVip")
public class StoreVipController {
    @Autowired
    IStoreVipService storeVipService;


    @GetMapping("/list")
    public R list(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                  @RequestParam(required = false, defaultValue = "") String keyword) {
        Page<StoreVip> storeVipPage = new Page<>(currentPage, pageSize);
        Page<StoreVip> page = storeVipService.page(storeVipPage, new LambdaQueryWrapper<StoreVip>().like(StoreVip::getName, keyword).eq(StoreVip::getIsDel, "0"));
        return R.ok(page);
    }

    @GetMapping("")
    public R getStoreVip(@RequestParam(required = true) Integer vipId) {
        return R.ok(storeVipService.getById(vipId));
    }

    @PostMapping()
//
    public R saveStoreVip(@RequestBody StoreVip storeVip) {
//        storeVip.setAddTime(((new Date().getTime())/1000));
        storeVip.setStoreId(1);
        storeVip.setIsClose(0);
        storeVip.setIsDel(0);

        if (storeVip.getSpecialDesc22() != null) {
            storeVip.setSpecialDesc(JSON.toJSONString(storeVip.getSpecialDesc22()));
        }

        storeVipService.save(storeVip);
        return R.ok("添加成功");
    }

    @PutMapping()
    public R updateStoreVip(@RequestBody StoreVip storeVip) {
        if (storeVip.getSpecialDesc22()!=null){

            String s = JSON.toJSONString(storeVip.getSpecialDesc22());
            try {
                storeVip.setSpecialDesc((String) storeVip.getSpecialDesc22());
            } catch (Exception e) {
                storeVip.setSpecialDesc(s);
                e.printStackTrace();
            }
        }

        storeVipService.updateById(storeVip);
        return R.ok("修改成功");
    }

    @DeleteMapping()
    public R deleteById(@RequestParam(required = true) Integer vipId) {

        storeVipService.update(new LambdaUpdateWrapper<StoreVip>().set(StoreVip::getIsDel, 1).eq(StoreVip::getId, vipId));
        return R.ok("删除成功");
    }

    @PutMapping("status")
    public R updateStatus(@RequestParam(required = true) Integer vipId) {
        StoreVip store = storeVipService.getById(vipId);
        if (store != null) {
            if (store.getIsClose() == 0) {
                store.setIsClose(1);
            } else {
                store.setIsClose(0);
            }
            storeVipService.updateById(store)
            ;
            return R.ok("操作成功");
        }

        return R.fail("操作失败");
    }

}
